<?php

/**
 * 增加日志
 * @param $log
 * @param bool $name
 */
function addlog($log, $name = false) {
    $Model = M('log');
    if (!$name) {
        session_start();
        $uid = session('uid');
        if ($uid) {
            $user = M('member')->field('user')->where(array('uid' => $uid))->find();
            $data['name'] = $user['user'];
        } else {
            $data['name'] = '';
        }
    } else {
        $data['name'] = $name;
    }
    $data['t'] = time();
    $data['ip'] = $_SERVER["REMOTE_ADDR"];
    $data['log'] = $log;
    $Model->data($data)->add();
}

/**
 *
 * 获取用户信息
 *
 * */
function member($uid, $field = false) {
    $model = M('Member');
    if ($field) {
        return $model->field($field)->where(array('uid' => $uid))->find();
    } else {
        return $model->where(array('uid' => $uid))->find();
    }
}

function getExcelObj($filePath, &$msg = "") {
    import("Org.Util.PHPExcel");
    $objPHPExcel = new \PHPExcel();
    import("Org.Util.PHPExcel.Reader.Excel2007");
    $PHPReader = new \PHPExcel_Reader_Excel2007(); //默认是excel2007
    //判断读取是否成功不成功则实例化2005的
    if (!$PHPReader->canRead($filePath)) {
        import("Org.Util.PHPExcel.Reader.Excel5");
        $PHPReader = new \PHPExcel_Reader_Excel5(); //如果不成功的时候用以前的版本来读取  
        if (!$PHPReader->canRead($filePath)) {
            $msg = '请选择需要导入的商品execl文件!';
            return false;
        }
    }
    //加载数据
    $PHPExcel = $PHPReader->load($filePath);
    return $PHPExcel;
}


/**
 * 通过外部渠道代码获得对应的商家名称
 * @param  $code $[name] [<description>]
 */
function customer_source( $code ) {
    if( !$code ) return '';
    $customer = M('customer')->where(array('code' => $code))->getField( 'shop_name' );
    return $customer ? $customer : $code;
}

/**
 * 身份证号码隐藏
 * @param  $str $[name] [<description>]
 */
function cardHidden( $str ) {
    return  substr_replace($str,str_repeat('*',6),6,7);
}

/**
 * 获得预警库存的状态值
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
function getStockStatus( $id ) {
    if( !$id ) return '';
    $status_data = array(
        5 => '盘存盈亏',
        6 => '补货',
        7 => '生产',
        8 => '供应商',
        9 => '客户',
        11 => 'WH',
        12 => '库存',
        18 => 'WH',
        21 => '播种库位',
        24 => '墙库位'
    );
    return isset( $status_data[$id] ) && $status_data[$id] ? $status_data[$id] : '';
}

/**
 * 权限默认选中操作
 */
function authMenuDefault($data , $id , $level = 1 ) {
    if( !$id ) return false;
    switch( $level ) {
        case 1 : 
                $str = '-'. $id . '|';
            break;
        case 2 :
                $str = '|' . $id . '|';
            break;
        case 3 :
                $str = '|' . $id . '=';
           break;
    }
    $data = explode( ',' , $data );
    foreach( $data as $key => $value ) {
        if( strpos( '-' . $value , $str ) !== false ) {
            return true;
        }
    }
    return false;
}

/**
 * 通过商品ID获得供应商数量
 * @param int $goods_id
 */
function getGoodsSupplyTotal( $goods_id ) {
    if( !$goods_id ) return '0';
    return M('goods_supply')->where(array('goods_id' => $goods_id , 'status' => 2))->count();
}

/**
 * 通过商品ID获得供应商供应库存总数
 * @param int $goods_id
 */
function getGoodsSupplyStockTotal( $goods_id ) {
    if( !$goods_id ) return '0';
    return M('goods_supply')->where(array('goods_id' => $goods_id , 'status' => 2))->sum('stock_num');
}